package com.glsc.ngateway.platform.controller.cmdb;

import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbCpuStandard;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.service.cmdb.CmdbCpuStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_CPU_STANDARD;
import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_WEB;

@RestController
@Api(value = "CMDB CPU Standard 管理", tags = {"CMDB CPU Standard 管理"})
public class CmdbCpuStandardController {

    @Autowired
    private CmdbCpuStandardService cmdbCpuStandardService;
    @Autowired
    private UserCache userCache;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/cpuStandard/search")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<Object> searchCpuStandard(@RequestBody Object params) {
        Object result = cmdbCpuStandardService.search(CMDB_CPU_STANDARD,params);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(value = "/cpuStandard/findByStandardCode")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<Object> findByStandardCode(@RequestParam String cpuStandardCode) {
        Object result = cmdbCpuStandardService.findByStandardCode(cpuStandardCode);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/cpuStandard/addOrUpdate")
    @ApiOperation(value = "修改 CPU Standard", notes = "修改 CPU Standard")
    public PlatformResponse<Void> addOrUpdateCpuStandard(@RequestBody CmdbCpuStandard cmdbCpuStandard) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbCpuStandardService.addOrUpdate(cmdbCpuStandard,account,CMDB_WEB);
        return PlatformResponse.successMsg("处理成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/cpuStandard/delete")
    @ApiOperation(value = "删除 CPU Standard", notes = "删除 CPU Standard")
    public PlatformResponse<Void> deleteCpuStandard(@RequestBody CmdbCpuStandard cmdbCpuStandard) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbCpuStandardService.delete(cmdbCpuStandard,account);
        return PlatformResponse.successMsg("删除成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/cpuStandard/deleteList")
    @ApiOperation(value = "批量删除 CPU Standard", notes = "批量删除 CPU Standard")
    public PlatformResponse<Void> deleteListCpuStandard(@RequestBody List<String> codes) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbCpuStandardService.deleteByCodeList(codes,account);
        return PlatformResponse.successMsg("删除成功");
    }
}


